package com.example.demo.controller;


import com.example.demo.entity.CodeMsg;
import com.example.demo.entity.apply;
import com.example.demo.entity.userAdd;
import com.example.demo.model.result;
import com.example.demo.services.applyService;
import com.example.demo.services.userSecServices;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 请假申请 我直接写到了数据库 没有用缓存
 */

@RestController
@RequestMapping("/apply")
public class addApplyController {

    @Autowired
    private applyService applyService;

     Logger logger= LoggerFactory.getLogger(getClass());

     @Autowired
     private userSecServices userSecServices;


    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    public result addApply( String name, String goTime, String endTime, String type, String sec, String teacher, Integer liveTime,String userId,Integer teacherId){
        logger.info("用户正在提交请假申请");
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime begin=LocalDateTime.parse(goTime,dateTimeFormatter);
        LocalDateTime end=LocalDateTime.parse(endTime,dateTimeFormatter);
        Integer test=applyService.addApply(name, begin, end, type, sec, teacher, liveTime, userId, teacherId);
        if(test!=1){
            return result.fail(CodeMsg.APPLY_ERROR);
        }else {
            return result.success("提交成功");
        }
    }

    //教职工查看 学生所有信息
    @RequestMapping(value = "/get",method = {RequestMethod.GET})
    public result getApply(Integer teacherId){
        logger.info("教职工正在查看学生请假申请");
        return result.success(userSecServices.selectall(teacherId));
    }


    //测试查询所有的 请假信息
    @RequestMapping(value = "/selectAll",method = {RequestMethod.GET})
    public result selectAll(){
        logger.info("正在测试查询连接数据库");
        return result.success(applyService.selectall());
    }

    //查看学生 请假信息
    @RequestMapping(value = "/studentget",method = {RequestMethod.GET})
    public result studentget(Integer teacherId){
        logger.info("教职工查询要审批的请假单");
        List<apply> test=applyService.studentSel(teacherId);
        if(test==null){
            return result.success("没有请假单");
        }else {
            return result.success(test);
        }
    }

    //教职工审批 通过就返回 1 反之为2 发送修改 数据库中的数据 3为未审批
    @RequestMapping(value = "/teacherSelete",method = {RequestMethod.POST})
    public result teacherSelete(Integer teacherId,Integer id,Integer type){
        logger.info("教职工审批中");
        Integer applyType=applyService.teacherSelete(teacherId, id,type);
        if(applyType==1){
            return result.success(applyType);
        }else {
            return result.fail("发送了未知错误");
        }
    }



    //学生查看自己所有的假条审批结果信息
    @RequestMapping(value = "/studetSelect",method = {RequestMethod.GET})
    public result studentSelect(String id){
        logger.info("学生查看审批结果");
        List<apply> test=applyService.student(id);
        if(test==null){
            return result.fail("没有数据");
        }else {
            return result.success(test);
        }
    }

    @RequestMapping(value = "/getAllStudent",method = {RequestMethod.GET})
    public result studentSId(Integer id){
        logger.info("学生正在查看对应的请假单号");
        return result.success(applyService.studentSelete(id));
    }

    //有多少份请假申请没有审批
    @RequestMapping(value = "/getCount",method = {RequestMethod.GET})
    public result getCount(@Param("teacherId")Integer teacherId){
        logger.info("有老师登录系统了");
        return result.success(applyService.countAll(teacherId));
    }

    //返回学校里面老师信息，供学生请假时选择
    @RequestMapping(value = "/selectteacher",method = {RequestMethod.GET})
    public result selectTeacher(){
        logger.info("查看老师信息");
        return result.success(applyService.selectTeacher());
    }


    //后台管理员查看 学校所有请假记录
    @GetMapping("/gliyuanApply")
    public result gliyuanApply() {
        logger.info("管理员正在查看学校学生请假记录");
        return result.success(applyService.students());
    }



    //学生端查看自己未通过的请假申请
    @GetMapping("/getNoApply")
    public result getNoApply(String userId,Integer page,Integer size) {
        logger.info("学生正在查看自己的请假失败申请反馈");
        return result.success(applyService.selectNoApply(userId, page, size));
    }

    //学生端查看自己的所有申请记录
    @GetMapping("/stugetAllApply")
    public result stugetAllApply(String userId,Integer page,Integer size){
        logger.info("学生正在查看自己的所有请假记录");
        return result.success(applyService.chazhaoUserId(userId, page, size));
    }




    //学生端请假表的table未通过申请信息
    @GetMapping("/getApplyResport")
    public result getApplyResport(Integer size,Integer page,String userId){
        logger.info("学生在请假页面");
        return result.success(applyService.selectAllreject(size,page,userId));
    }


    @DeleteMapping("/deleteapply/{id}")
    public result deleteApply(@PathVariable("id")Integer id){
        logger.info("学生");
        return result.success(applyService.deleteApply(id));
    }









}
